<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE富文本流式输出演示</title>
    <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='75' font-size='75' fill='%23667eea'>📄</text></svg>">
    <!-- markdown-it库用于Markdown解析 -->
    <script src="https://cdn.jsdelivr.net/npm/markdown-it/dist/markdown-it.min.js"></script>
    <!-- highlight.js用于代码高亮 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
        }

        .header {
            text-align: center;
            color: white;
            margin-bottom: 30px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }

        .tabs {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            background: white;
            border-radius: 12px;
            padding: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            flex-wrap: wrap;
        }

        .tab-btn {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            background: #f0f0f0;
            color: #333;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .tab-btn.active {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .tab-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .card {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            display: none;
        }

        .card.active {
            display: block;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .controls {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }

        button {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            font-size: 1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .btn-start {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            flex: 1;
            min-width: 150px;
        }

        .btn-start:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
        }

        .btn-start:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .btn-clear {
            background: #f0f0f0;
            color: #333;
            flex: 0 0 auto;
        }

        .btn-clear:hover {
            background: #e0e0e0;
        }

        .status {
            padding: 12px 15px;
            border-radius: 6px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 500;
        }

        .status.idle {
            background: #f0f0f0;
            color: #666;
        }

        .status.loading {
            background: #e3f2fd;
            color: #1976d2;
        }

        .status.done {
            background: #e8f5e9;
            color: #388e3c;
        }

        .status.error {
            background: #ffebee;
            color: #d32f2f;
        }

        .status::before {
            content: '';
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            flex-shrink: 0;
        }

        .status.loading::before {
            background: #1976d2;
            animation: pulse 1.5s infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }

        /* Markdown样式 */
        .content {
            line-height: 1.8;
            color: #333;
        }

        .content h1 {
            font-size: 2em;
            margin: 30px 0 20px;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }

        .content h2 {
            font-size: 1.6em;
            margin: 25px 0 15px;
            color: #667eea;
        }

        .content h3 {
            font-size: 1.3em;
            margin: 20px 0 10px;
            color: #764ba2;
        }

        .content p {
            margin: 10px 0;
            text-align: justify;
        }

        .content code {
            background: #f5f5f5;
            padding: 2px 6px;
            border-radius: 3px;
            font-family: 'Courier New', monospace;
            color: #d32f2f;
        }

        .content pre {
            background: #0a1929;
            color: #64ffda;
            padding: 15px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 15px 0;
            border-left: 4px solid #667eea;
        }

        .content pre code {
            background: none;
            color: inherit;
            padding: 0;
            font-size: 0.9em;
        }

        .content ul, .content ol {
            margin: 15px 0 15px 30px;
        }

        .content li {
            margin: 8px 0;
        }

        .content a {
            color: #667eea;
            text-decoration: none;
            border-bottom: 1px solid #667eea;
            transition: all 0.3s ease;
        }

        .content a:hover {
            color: #764ba2;
            border-bottom-color: #764ba2;
        }

        .content img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin: 15px 0;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            max-height: 350px;
        }

        .content blockquote {
            border-left: 4px solid #667eea;
            padding-left: 15px;
            margin: 15px 0;
            color: #666;
            font-style: italic;
            background: #f8f9fa;
            padding: 10px 15px;
            border-radius: 4px;
        }

        .content table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }

        .content table th, .content table td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }

        .content table th {
            background: #667eea;
            color: white;
            font-weight: 600;
        }

        .content table tr:nth-child(even) {
            background: #f8f9fa;
        }

        .content table tr:hover {
            background: #e8ecf1;
        }

        .content hr {
            border: none;
            height: 2px;
            background: linear-gradient(90deg, transparent, #667eea, transparent);
            margin: 30px 0;
        }

        /* 结构化JSON内容样式 */
        .rich-item {
            margin: 15px 0;
        }

        .rich-item.text p {
            margin: 0;
        }

        .rich-item.code {
            background: #0a1929;
            color: #64ffda;
            padding: 15px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            overflow-x: auto;
        }

        .rich-item.code pre {
            margin: 0;
            background: none;
        }

        .rich-item.link a {
            display: inline-block;
            padding: 8px 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 6px;
            text-decoration: none;
            border: none;
            transition: all 0.3s ease;
        }

        .rich-item.link a:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .rich-item.image {
            text-align: center;
        }

        .rich-item.image img {
            max-width: 100%;
            max-height: 300px;
            height: auto;
            border-radius: 8px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .rich-item.blockquote {
            border-left: 4px solid #667eea;
            padding-left: 15px;
            color: #666;
            font-style: italic;
        }

        .output-wrapper {
            max-height: 600px;
            overflow-y: auto;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            padding: 20px;
            background: #fafafa;
        }

        .output-wrapper::-webkit-scrollbar {
            width: 8px;
        }

        .output-wrapper::-webkit-scrollbar-track {
            background: #f0f0f0;
        }

        .output-wrapper::-webkit-scrollbar-thumb {
            background: #667eea;
            border-radius: 4px;
        }

        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin-top: 20px;
        }

        .stat-item {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
        }

        .stat-item strong {
            color: #666;
            display: block;
            margin-bottom: 5px;
            font-size: 0.9em;
        }

        .stat-value {
            font-size: 1.5em;
            color: #667eea;
            font-weight: bold;
        }

        @media (max-width: 768px) {
            .header h1 {
                font-size: 1.8em;
            }

            .content h1 {
                font-size: 1.5em;
            }

            .tabs {
                flex-direction: column;
            }

            .tab-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📝 SSE富文本流式输出演示</h1>
            <p>支持Markdown、代码、链接、图片等多种内容类型</p>
        </div>

        <div class="tabs">
            <button class="tab-btn active" onclick="switchTab('markdown')">📄 Markdown流</button>
        </div>

        <!-- Markdown标签页 -->
        <div class="card active" id="markdown">
            <div class="controls">
                <button class="btn-start" onclick="startMarkdownStream()">▶️ 开始Markdown流</button>
                <button class="btn-clear" onclick="clearContent('markdown-output')">🗑️ 清空</button>
            </div>
            <div id="markdown-status" class="status idle">就绪</div>
            <div class="output-wrapper">
                <div class="content" id="markdown-output"></div>
            </div>
            <div class="stats">
                <div class="stat-item">
                    <strong>字符数</strong>
                    <span class="stat-value" id="markdown-chars">0</span>
                </div>
                <div class="stat-item">
                    <strong>耗时</strong>
                    <span class="stat-value" id="markdown-time">0ms</span>
                </div>
            </div>
        </div>



    </div>

    <script type="module">
        // 使用 markdown-it 替代 marked
        const md = window.markdownit({
            html: false,
            linkify: true,
            typographer: true,
            highlight: function (str, lang) {
                if (lang && hljs.getLanguage(lang)) {
                    return hljs.highlight(str, { language: lang }).value;
                }
                return '';
            }
        });

        let startTime = null;

        // 将所有全局函数挂载到window上
        window.switchTab = function(tabName) {
            // 隐藏所有卡片
            document.querySelectorAll('.card').forEach(card => {
                card.classList.remove('active');
            });
            // 移除所有按钮的active类
            document.querySelectorAll('.tab-btn').forEach(btn => {
                btn.classList.remove('active');
            });
            
            // 显示选中的卡片
            document.getElementById(tabName).classList.add('active');
            event.target.classList.add('active');
        };

        function updateStatus(elementId, status, message) {
            const el = document.getElementById(elementId);
            el.className = 'status ' + status;
            el.textContent = message;
        }

        function updateTime(elementId, startTime) {
            if (startTime) {
                const duration = Date.now() - startTime;
                document.getElementById(elementId).textContent = duration + 'ms';
            }
        }

        window.clearContent = function(elementId) {
            document.getElementById(elementId).innerHTML = '';
        };

        // Markdown流处理
        window.startMarkdownStream = function() {
            const outputId = 'markdown-output';
            clearContent(outputId);
            startTime = Date.now();
            
            updateStatus('markdown-status', 'loading', '连接中...');
            
            const eventSource = new EventSource('/test/rich-stream/markdown-stream');
            const output = document.getElementById(outputId);

            eventSource.addEventListener('message', (event) => {
                try {
                    const markdown = event.data;
                    // 使用markdown-it将Markdown转换为HTML
                    const html = md.render(markdown);
                    output.innerHTML = html;
                    
                    document.getElementById('markdown-chars').textContent = markdown.length;
                    updateTime('markdown-time', startTime);
                    updateStatus('markdown-status', 'loading', '接收中...');
                    
                    // 代码高亮
                    output.querySelectorAll('pre code').forEach(block => {
                        hljs.highlightElement(block);
                    });
                } catch (e) {
                    console.error('Markdown解析错误:', e);
                }
            });

            eventSource.addEventListener('done', () => {
                updateStatus('markdown-status', 'done', '完成！');
                eventSource.close();
            });

            eventSource.addEventListener('error', (e) => {
                updateStatus('markdown-status', 'error', '连接错误');
                eventSource.close();
            });
        };

        // 代码流处理
        window.startCodeStream = function() {
            const outputId = 'code-output';
            clearContent(outputId);
            startTime = Date.now();
            
            updateStatus('code-status', 'loading', '连接中...');
            
            const eventSource = new EventSource('/test/rich-stream/markdown-stream');
            const output = document.getElementById(outputId);

            eventSource.addEventListener('message', (event) => {
                try {
                    const code = event.data.replace(/\\n/g, '\n');
                    output.textContent = code;
                    
                    // 重新高亮
                    hljs.highlightElement(output);
                    
                    document.getElementById('code-chars').textContent = code.length;
                    updateTime('code-time', startTime);
                    updateStatus('code-status', 'loading', '接收中...');
                    
                    // 自动滚动到底部
                    output.parentElement.parentElement.scrollTop = output.parentElement.parentElement.scrollHeight;
                } catch (e) {
                    console.error('代码处理错误:', e);
                }
            });

            eventSource.addEventListener('done', () => {
                updateStatus('code-status', 'done', '完成！');
                eventSource.close();
            });

            eventSource.addEventListener('error', () => {
                updateStatus('code-status', 'error', '连接错误');
                eventSource.close();
            });
        };
    </script>
</body>
</html>
